/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webaccess.retailws.Action;

import com.webaccess.retailws.Delegate.MenuDelegate;
import com.webaccess.retailws.Util.CommonUtil;
import com.webaccess.retailws.Util.menuUtil;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author Windows
 */
public class NewsLetterDispatchAction extends DispatchAction {

    private String SUCCESS = "success";
    ActionMessages errors = null;
    MenuDelegate menuDelegateObj = null;
    menuUtil menuUtilObj = null;
    CommonUtil commUtil = new CommonUtil();

    public ActionForward unsubscribeNewsletter(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        menuDelegateObj = new MenuDelegate();
        menuUtilObj = new menuUtil();
        errors = new ActionMessages();
        String newsletterEmail = "";
        Long newsLetterId = 0L;
        try {
            commUtil.getTopRightListMap(request);

            DynaActionForm nlEmailDynaForm = (DynaActionForm) form;
            newsletterEmail = String.valueOf(nlEmailDynaForm.get("newsLetterEmail"));
            newsLetterId = menuDelegateObj.checkEmail(newsletterEmail);
            Pattern emailExp = Pattern.compile("/^[\\w\\-\\.\\+]+\\@[a-zA-Z0-9\\.\\-]+\\.[a-zA-z0-9]{2,4}$/");
            Matcher matchEmailId = emailExp.matcher(newsletterEmail);

            if (newsletterEmail.equalsIgnoreCase("")) {
                errors.add("errNewsLetter", new ActionMessage("errors.newsletter.required"));
            } else if (matchEmailId.find()) {
                errors.add("errNewsLetter", new ActionMessage("errors.emailid.invalid"));
            } else if (newsLetterId > 0) {
                SUCCESS = menuUtilObj.subscribeNL(newsLetterId, newsletterEmail);
            }

            saveErrors(request, errors);
            if (errors.isEmpty()) {
                SUCCESS = "homepageshow";
            } else {
                SUCCESS = "homepageshow";
            }

        } catch (Exception ex) {
            SUCCESS = "unsubscribefailure";
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward subscribeNewsletter(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        menuDelegateObj = new MenuDelegate();
        menuUtilObj = new menuUtil();
        errors = new ActionMessages();
        String newsletterEmail = "";
        try {
            commUtil.getTopRightListMap(request);

            DynaActionForm nlEmailDynaForm = (DynaActionForm) form;
            newsletterEmail = String.valueOf(nlEmailDynaForm.get("newsLetterEmail"));

            Pattern emailExp = Pattern.compile("/^[\\w\\-\\.\\+]+\\@[a-zA-Z0-9\\.\\-]+\\.[a-zA-z0-9]{2,4}$/");
            Matcher matchEmailId = emailExp.matcher(newsletterEmail);

            if (newsletterEmail.equalsIgnoreCase("")) {
                errors.add("errNewsLetter", new ActionMessage("errors.newsletter.required"));
            } else if (matchEmailId.find()) {
                errors.add("errNewsLetter", new ActionMessage("errors.emailid.invalid"));
            } else if (menuDelegateObj.checkEmail(newsletterEmail) > 0) {
                errors.add("errNewsLetter", new ActionMessage("errors.newsletter.exist"));
            } else {
                SUCCESS = menuUtilObj.subscribeNL(0L, newsletterEmail);
            }
            saveErrors(request, errors);
            if (errors.isEmpty()) {
                SUCCESS = "homepageshow";
            } else {
                SUCCESS = "homepageshow";
            }
        } catch (Exception ex) {
            SUCCESS = "homepageshow";
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }
}
